﻿/*
Copyright (c) 2010, hiSoft
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the hiSoft nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE HISOFT AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL HISOFT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using Selenium;

namespace Orchard.Test.Library
{
    public abstract class UIHelperBase
    {
        protected TestLibrary TestSettings { get; private set; }
        protected ISelenium selenium { get { return TestSettings.Selenium; } }

        protected const string ContentItemsLocator = "//fieldset[@class='contentItems bulk-items']/ul/li";
        protected const string PostsLocator = "//fieldset[@class='contentItems bulk-items']/ul/li";
        protected const string BlogsLocator = "//fieldset[@class='contentItems bulk-items']/ul/li";
        protected const string CommentsLocator = "//table[@class='items']/tbody/tr";
        protected const string ContentTypesLocator = "//ul[@class='contentItems']/li";
        protected const string ContentPartsLocator = "//ul[@class='contentItems']/li";
        protected const string PartsLocatorForAddParts = "//ul[@class='available-parts']/li";
        protected const string UsersLocator = "//table[@class='items']/tbody/tr";
        protected const string RolesLocator = "//table[@class='items']/tbody/tr";
        //protected const string RolesLocatorForAddUser = "//div[@class='edit-item-content']/fieldset[5]/div";
        protected const string RolesLocatorForAddUser = "//div[@id='content']/div/form/div/div/div/fieldset[5]/div";
        protected const string RolesLocatorForEditUser = "//div[@class='edit-item-content']/fieldset[3]/div";
        protected const string TagsLocator = "//table[@class='items']/tbody/tr";
        protected const string ThemesLocator = "//ul[@class='templates']/li";
        protected const string MenusLocator = "//table[@class='items']/tbody/tr";
        protected const string GalleryFeedsLocator = "//table[@class='items']/tbody/tr";
        protected const string TenantsLocator = "//ul[@class='contentItems tenants']/li";
        protected const string WidgetLayersLocator = "//select[@id='layerId']/option";
        protected const string WidgetZonesLocator = "//div[@id='widgets-zones']/ol/li";
        protected const string SiteCulturesLocator = "//ul[@class='site-cultures']/li";
        protected const string ModulesPermissionLocator = "//form[@method='post']/fieldset[2]/fieldset";
        protected const string ModulesListLocatorInGallery = "//div[@id='content']/div/form/ul/li";
        protected const string ListItemListLocator = "//div[@id='content']/div/form/fieldset[3]/ul/li";

        protected const string OwnerLocator = "OwnerEditor_Owner";
        protected const string CreatedOnDateLocator = "DateEditor_CreatedDate";
        protected const string CreatedOnTimeLocator ="DateEditor_CreatedTime";

        protected UIHelperBase(TestLibrary settings)
        {
            TestSettings = settings;
        }
        
        public static string GenerateUniqueIdentifier(DateTime date)
        {
            return string.Format("{0}{1}{2}{3}{4}{5}",
                                date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second);
        }

        protected static string GenerateUniqueIdentifier()
        {
            return GenerateUniqueIdentifier(DateTime.Now);
        }      

        protected void ClickAndWait(string locator)
        {
            selenium.Click(locator);
            selenium.WaitForPageToLoad(TestLibrary.Consts.TimeToWaitForPageToLoad);
        }

        protected void ClickAndWaitForLongTime(string locator)
        {
            selenium.Click(locator);
            selenium.WaitForPageToLoad(TestLibrary.Consts.LongTimeToWaitForPageToLoad);
        }

        protected void Submit()
        {
            if (selenium.IsElementPresent("//input[@type='submit']"))
            {
                ClickAndWait("//input[@type='submit']");
            }
            else if (selenium.IsElementPresent("//button[@type='submit']"))
            {
                if (selenium.IsElementPresent("//button[@name='submit.Publish']"))
                {
                    ClickAndWait("//button[@name='submit.Publish']");
                }
                else
                {
                    ClickAndWait("//button[@type='submit']");
                }
            }
        }      
    }
}
